package bank.model.vo;

import java.util.ArrayList;
import java.util.List;

public class Payment extends Document {
	private Account senderAccount;
	private Account recieverAccount;
	private int amount;
	private int hash;
	List codes = new ArrayList();

	Terminal getTerminal;

	Terminal putTerminal;

	public Payment(Employee employee, Account senderAccount, Account recieverAccount,
			int amount) {
		super(employee);
		senderAccount.setSourceAccount(senderAccount);
		recieverAccount.setRecieverAccount(recieverAccount);
		this.amount = amount;
		hash = this.hashCode();
	}

	public int getAmount() {
		return amount;
	}

	public int getHash() {
		return hash;
	}

	public boolean doGetTransaction(Account senderAccount, int amount) {
		this.senderAccount = senderAccount;
		this.amount = amount;
		boolean get;
		getTerminal = new GetTerminal(amount);
		getTerminal.transaction();
		if (getTerminal.getCurAmount() == 0) {
			get = false;
		} else
			get = true;
		return get;
	}

	private class GetTerminal implements Terminal {
		int curAmount;

		int money;

		public int getCurAmount() {
			return curAmount;
		}

		GetTerminal(int amount) {
			curAmount = amount;
			this.hashCode();
			codes.add(this);
		}

		public boolean transaction() {
			money = senderAccount.getMoney();
			boolean transactionSuccessfull = false;
			if (commit()) {
				money =- curAmount;
				transactionSuccessfull = true;
			} else {
				rollback();
			}
			return transactionSuccessfull;
		}

		private boolean commit() {
			if (money >= curAmount)
				return true;
			else
				return false;
		}

		private void rollback() {
			curAmount = 0;
		}

		public int getHash() {
			return hash;
		}
	}

	public boolean doPutTransaction(Account recieverA) {
		recieverAccount = recieverA;
		putTerminal = new PutTerminal(amount);
		return putTerminal.transaction();
	}

	private class PutTerminal implements Terminal {
		int curAmount;
		int money;

		public PutTerminal(int amount) {
			curAmount = amount;
		}

		public int getCurAmount() {
			return curAmount;
		}

		public boolean transaction() {
			recieverAccount.setMoney(curAmount);
			return true;
		}
	}

	/*
	 * private void commit() { payment.add(this);
	 *  } private void rollback(int h) { hash=h;
	 * 
	 * 
	 *  }
	 */

	public void doPayment() {
		if (doGetTransaction(senderAccount, amount)) {
			doPutTransaction(recieverAccount);
		}
		// if (flag){commit();}

		// else rollback();

	}

	protected void generateLines() {
		addLine(String.format("=============== Payment %d ===============\n",
				getId())); 
		addLine(String.format("Sender account: %d\n", senderAccount.getId())); 
		addLine(String.format("Reciever account: %d\n", recieverAccount.getId())); 
		addLine(String.format("Bank employee: %s\n", getEmployee().toString()));
		addLine(String.format("Date: %s\n",	getCreationDate().toString()));
	}
}
